//
//  LZTStatusOriginalView.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/13.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit
import SnapKit

class LZTStatusOriginalView: UIView {
    
    var originalViewBotomContraint:Constraint?
    
    //通过viewModel获取数据
    var statusViewModel: LZTStatusViewModel? {
        
        didSet{
            headImageView.lzt_setImageWithString(statusViewModel?.statusModel?.user?.profile_image_url, placeHolderImgName: "avatar_default")
            
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            
            //时间label 来源label
            timeLabel.text = statusViewModel?.sinaTimeStr
            //通过VM进行获取 来源
            sourceLabel.text = statusViewModel?.sinaSourceStr
            
            //微博正文
            contentLabel.attributedText = statusViewModel?.originalAttr
            
            //等级 - 通过viewModel获取image
            membershipImageView.image = statusViewModel?.mbrankImage
            
            //认证 - 通过viewModel获取image
            avatarImageView.image = statusViewModel?.verifiedImage
            
            //判断配图的显示
            //卸载约束
            originalViewBotomContraint?.uninstall()
            
            if statusViewModel?.statusModel?.pic_urls?.count > 0 {
                
                //有配图
                //赋值
                pictureView.pictureInfos = statusViewModel?.statusModel?.pic_urls
                
                //根据 contentLabel的内容进行设置cell的行高
                self.snp_makeConstraints { (make) in
                    originalViewBotomContraint = make.bottom.equalTo(pictureView).offset(10).constraint
                }
                //显示配图
                pictureView.hidden = false
                
            } else {
                // 无配图
                self.snp_makeConstraints { (make) in
                    originalViewBotomContraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
                //隐藏配图
                pictureView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置试图
    private func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        //添加约束
        headImageView.snp_makeConstraints { (make) in
            make.leading.top.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        membershipImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(10)
        }
        pictureView.snp_makeConstraints { (make) in
            //宽高应该在picture赋值时进行设置
            //make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((SCREENW - 250) * 0.5)
        }
        
        //根据 contentLabel的内容进行设置cell的行高
        //默认一上来;全部有配图
        self.snp_makeConstraints { (make) in
            originalViewBotomContraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
    }
    
    //懒加载控件
    //头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    //名字
    private lazy var nameLabel: UILabel = {
        
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.darkGrayColor()
        return lab
    }()
    //等级
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    //时间
    private lazy var timeLabel: UILabel = UILabel(textColor: AppThemeColor, textSize: 10)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textSize: 10)
    //认证 avatar_vgirl
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //微博正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 15, maxWidth: SCREENW - 20)
    //配图
    private lazy var pictureView: LZTStatusPictureView = LZTStatusPictureView()
}
